package com.systop.controller;

import com.systop.entity.SysUser;
import com.systop.utils.BizCodeEnum;
import com.systop.service.SysUserService;
import com.systop.utils.R;
import com.systop.validator.group.AddGroup;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    //Home页面人员管理查询并分页功能
    @RequestMapping(value = "/findAllUser",method=RequestMethod.GET)
    @ResponseBody
    public R findAllUser(SysUser sysUser){
        //定义键值对返回数据
        Map<String,Object> map = new HashMap<String,Object>();
        //返回数据总条数
        int pageCount = sysUserService.countAllUser(sysUser);

        // System.out.println(pageCount);
        //分页计算
        sysUser.setStart((sysUser.getStart()-1)*sysUser.getPageSize());
        List list = sysUserService.findAllUser(sysUser);
        if(CollectionUtils.isEmpty(list) && sysUser.getStart()>0){
            //设置当本页没有数据时返回上一页
            sysUser.setStart(sysUser.getStart()/sysUser.getPageSize());
            //设置当本页没有数据时返回第一页
            return findAllUser(sysUser);
        }

        map.put("userlist",list);
        map.put("pagecount",pageCount);
        return R.ok(map);
    }

    //根据id删除指定对象方法
    @RequestMapping(value = "/delUser",method = RequestMethod.DELETE)
    @ResponseBody
    public R delUser(SysUser sysUser){
        // System.out.println(sysUser);
        //定义键值对返回数据
        Map<String,Object> map = new HashMap<String,Object>();
        int i = 0;
        //根据用户id去查询收货人表是否存在对应收货人，如果存在就先删除收货人再删除用户
        int exist = sysUserService.findConsigneeByUserId(sysUser);
        if(exist > 0){
            return R.error(
                    BizCodeEnum.HAS_CONSIGNEE.getCode(),
                    BizCodeEnum.HAS_CONSIGNEE.getMsg());
        }

        //删除是否执行成功
        i = sysUserService.delUser(sysUser);
        if(i==0){
            return R.error(
                    BizCodeEnum.DEL_FAIL.getCode(),
                    BizCodeEnum.DEL_FAIL.getMsg());
        }
        map.put("delSuccNum",i);
        return R.ok(map,BizCodeEnum.DEL_SUCCESS.getMsg());
    }

    //添加数据方法
    @RequestMapping(value="/addUser",method = RequestMethod.POST)
    @ResponseBody
    public R addUser(@Validated({AddGroup.class}) @RequestBody SysUser sysUser){
        Map<String,Object> map = new HashMap<String,Object>();

        //检查新增用户是否存在
        int exist = sysUserService.findByUserCode(sysUser);
        map.put("exist", exist);
        if(exist > 0){
            return R.error(
                    BizCodeEnum.USER_EXIST.getCode(),
                    BizCodeEnum.USER_EXIST.getMsg());
        }

        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20); //获取盐
        // System.out.println("加密前:"+admin.getMemberPass());
        sysUser.setUserPass(new Sha256Hash(sysUser.getUserPass(),salt).toHex());
        // System.out.println("加密后:"+admin.getMemberPass());
        sysUser.setSalt(salt);

        int i  = sysUserService.addUser(sysUser);
        map.put("addSuccNum",i);
        if(i<1){
            return R.error(
                    BizCodeEnum.INVALID_ADD_USER.getCode(),
                    BizCodeEnum.INVALID_ADD_USER.getMsg());
        }
        return R.ok(map);
    }

    //更新数据方法
    @RequestMapping(value = "/udpUser",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> udpUser(@RequestBody SysUser sysUser){
        Map<String,Object> map = new HashMap<String,Object>();
        int i  = sysUserService.udpUser(sysUser);
        map.put("udpSuccNum",i);
        if(i>0){
            map.put("msg", "修改成功!");
        }else{
            map.put("msg","修改失败,数据错误");
        }
        return map;
    }
}
